package com.example.workmanagerdemo;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
 * @author: tuyrt7 on 2020/9/7.
 */
public class UploadLogWorker extends Worker {
    public static final String OUTPUT_DATA = "output_data";

    public UploadLogWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    /**
     * 耗时任务在 doWork() 中执行
     */
    @NonNull
    @Override
    public Result doWork() {
        //获取传递进来的参数
        String inputData = getInputData().getString(MainActivity.INPUT_DATA);
        Log.d("KKKKK", "UploadLogWorker doWork inputData: " + inputData);

        //执行...
        int data = TestData.getInstance().getData();
        if (data == -1) {
            Log.d("KKKKK", "doWork failure" + data);
            return Result.failure();
        } else {
            Log.d("KKKKK", "doWork success=" + data);

            //任务执行完成，返回数据
            Data outputData = new Data.Builder()
                    .putString(OUTPUT_DATA, "Task Success!")
                    .build();

            return Result.success(outputData);
        }
    }
}
